<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.wxtf.platform.plugins.user.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
UserBean user = (UserBean)request.getSession().getAttribute("userBean");

String _db = user.getDbType();
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<base href="<%=basePath%>" />
	<meta http-equiv="X-UA-Compatible" content="IE=edge" />
	<meta http-equiv="pragma" content="no-cache" />
	<meta http-equiv="cache-control" content="no-cache" />
	<meta http-equiv="expires" content="0" />
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

	<link type="text/css" rel="stylesheet" href="ligerUI/skins/Aqua/css/ligerui-all.css" />
	<link type="text/css" rel="stylesheet" href="ligerUI/skins/<%=user.getUserCss()%>/css/ligerui-all.css" />
	<link type="text/css" rel="stylesheet" href="resource/css/style.css" />
	
	<script type="text/javascript" src="js/jquery/jquery-1.7.2.js"></script>
	<script type="text/javascript" src="ligerUI/js/core/base.js"></script>
	<script type="text/javascript" src="ligerUI/js/ligerui.min.js"></script>
	<script type="text/javascript" src="js/common.js"></script>
	<script type="text/javascript" src="js/validata.js"></script>
	
	<script type="text/javascript" src="ligerUI/js/plugins/ligerGrid.js"></script>
	<script type="text/javascript" src="ligerUI/js/plugins/ligerForm.js"></script>
	
	<script type="text/javascript">
		var _db = '<%=_db%>';
		
		_jsonObj.classname = "com.wxtf.platform.plugins.org.OrgAction";
		_jsonObj.methodname = "queryPage";
		_jsonObj.extenddata = '';
		var manager;
      	$(function(){
			manager = $("#user-grid").ligerGrid({
			url: _url, parms: _jsonObj,enabledSort:false,
	              height:'100%', width:'100%',usePager:false,rownumbers:true,
	              alternatingRow: true, tree: { columnName: 'text' },
	              columns: [
		            { display: 'id', name: 'id', width:1,hide: true },
		            { display: 'dparent_id', name: 'dparent_id', width:1,hide: true },
		            { display: '子节点个数', name: 'sons', width:1,hide: true },
		            { display: '部门名称', name: 'text', width: 300, align: 'left' },
		            { display: '部门简码', name: 'logic_code', width:80, align: 'left' },
		            { display: '负责人', name: 'director_name', width:80, align: 'left' },
		            { display: '电话', name: 'director_phone', width:80, align: 'left' },
		            { display: '手机', name: 'director_mobile', width:100, align: 'left' },
		            { display: '邮箱', name: 'director_email', width:80, align: 'left' },
           			{ display: '简介', name: 'action', width: 1, hide: true, align: 'left' },
           			{ display: '级别', name: 'levels', width: 1, hide: true, align: 'left' },
	              	{ display: '新建',name:'add', width:60,render:function(record, index, value, oth){
	              			var str = '\'' + record.id + '\',\'' + record.text + '\',\'' + record.sons + '\',\'' + record.levels + '\'';
	              			return '<a href="javascript:add(' + str + ')" class="edit_btn_div" onmouseover="this.className=\'edit_btn_div_over\'" onmouseout="this.className=\'edit_btn_div\'"><img src="ligerUI/skins/icons/add.gif"/></a>';
	              		}
	              	},
              		{ display: '编辑', name:'edit',width:60,render: function(record, index, value, oth){
							return "<img src='ligerUI/skins/icons/edit.gif' onclick='edit(" + JSON.stringify(record) + ")'></img>";
						}
					},
	              	{ display: '删除', name:'delete',width:60,render:function(record, index, value, oth){
	              			if(record.dparent_id == 0){
	              				return '';
	              			}
	              			return '<a href="javascript:delData(\'' + record.id + '\')"  class="edit_btn_div" onmouseover="this.className=\'edit_btn_div_over\'" onmouseout="this.className=\'edit_btn_div\'"><img src="ligerUI/skins/icons/delete.gif"/></div>';
	              		}
	              	},
	              	{ display: '选择人员', name:'edit',width:100,render:function(record, index, value, oth){
	              			if(record.dparent_id == 0){
	              				return '';
	              			}
	              			return '<a href="javascript:manage(\'' + record.id + '\')"  class="edit_btn_div" onmouseover="this.className=\'edit_btn_div_over\'" onmouseout="this.className=\'edit_btn_div\'"><img src="ligerUI/skins/icons/memeber.gif"/></div>';
	              		}
	              	}
	              ],
	              onClickRows: function (data, rowindex, rowobj){
	              }
	          });
	      });
		function reload(){
    		_jsonObj.methodname = "queryPage";
			_jsonObj.extenddata = '[{"id":0}]';
			manager.loadData();
		}
    	var win1;
       	//打开窗口	        
        function f_openWindow1(target, title, width, height, func, func2) {
			if(win1==null){
            	win1 = initWindowByDiv(target, title, width, height, func, func2);
			}else{
				win1.set({title:title});
				win1.show();
			}
       	}
       	var win2;
       	//打开窗口	        
        function f_openWindow2(target, title, width, height, func) {
			if(win2==null){
            	win2 = initWindowByDiv2(target, title, width, height, func);
			}else{
				win2.show();
			}
       	}
		function add(id, text, ordernum, level){
			document.forms[0].reset();
			
			var cnt = 10 * (parseInt(ordernum) + 1)
			$("#dparent_id").val(id);
			$("#id").val('');
			$("#ordernum").val(cnt);
			$("#levels").val(parseInt(level) + 1);
			
			f_openWindow1('editDiv', '为【'+text+'】添加下属部门', 400, 260, save, closeWin);
		}
		function edit(row){
			document.forms[0].reset();
			loadForm("mform", row);
			f_openWindow1('editDiv', '编辑', 400, 300,save, closeWin);
		}
		//保存机构
		function save(item, dialog) {
			var text = $('#text').val().trim();
			var logic_code = $('#logic_code').val().trim();
			var director_name = $('#director_name').val().trim();
			var director_phone = $('#director_phone').val().trim();
			var director_mobile = $('#director_mobile').val().trim();
			var director_email = $('#director_email').val().trim();
			var action = $('#action').val().trim();
			var ordernum = $('#ordernum').val().trim();
			
			if('' == text){warningTip("部门名称：不能为空"); return;}
			if('' == logic_code){warningTip("部门简码：不能为空"); return;}
			if(!checkIsString(text)){
				warningTip("部门名称：必须为字母、数字、下划线、中文组成");
				return;
			}
			if(fucCheckLength(text) > 50){
				warningTip("部门名称：不能超过50字符");
				return;
			}
			if(!checkIsStrNum(logic_code)){
				warningTip("部门简码：必须为字母、数字组成");
				return;
			}
			if(fucCheckLength(logic_code) > 50){
				warningTip("部门简码：不能超过50字符");
				return;
			}
			if(!checkIsString(director_name)){
				warningTip("负责人：必须为字母、数字、下划线、中文组成");
				return;
			}
			if(fucCheckLength(director_name) > 32){
				warningTip("负责人：不能超过50字符");
				return;
			}
			if(!checkIsTelephone(director_phone)){
				warningTip("电话：格式应为12345678、0431-12345678、0431-12345678-12345");
				return;
			}
			if(!checkIsMobile(director_mobile)){
				warningTip("手机：请输入正确的11位手机号");
				return;
			}
			if(!checkIsEmail(director_email)){
				warningTip("邮箱：格式为user@domain.com");
				return;
			}
			if(!isNum(ordernum)){
				warningTip("排序号：必须为整数");
				return;
			}
			if(-999999 > parseInt(ordernum) || parseInt(ordernum) > 999999){
				warningTip("排序号：必须在-999999~999999之间");
				return;
			}
			
			var id = $('#id').val();
			if(id == ''){
				_jsonObj.methodname = "save";
			}else{
				_jsonObj.methodname = "update";
			}
			_jsonObj.logicdata = getForm("mform");
			callserver(_jsonObj, function(data, satus){
				if(data.result){
					dialog.hide();
				}
				succMsgTip(data.resultrecord);
				reload();
			},function(data, satus){
				warningTip(data.resultrecord);
			});
		}
		function delData(id){
			$.ligerDialog.confirm('确定要删除选中数据（包括其子节点）吗？\n删除后不可恢复！', function (ml) { 			
				if(ml){
					_jsonObj.methodname = "delete";
					_jsonObj.logicdata = "[{\"ids\":\"" + id + "\"}]";
					callserver(_jsonObj, function(data, satus){
						succMsgTip(data.resultrecord);
						reload();
					},function(data, satus){
						warningTip(data.resultrecord);
					});
				}
			});
		}
		
		//人员管理
		function manage(id) {
			f_openWindow2('orgUser', '部门成员', 800, 450, function(){
				win2.hide(); 
			});
			listOrgUser(id);
		}
		
		var userjsonObj1 = { 
			"classname": "",
			"methodname": "",
			"logicdata": [],
			"extenddata": []
		};
		var userjsonObj2 = { 
			"classname": "",
			"methodname": "",
			"logicdata": [],
			"extenddata": []
		};
		//部门成员管理
		var userManager1;
		var userManager2;
       	function listOrgUser(val){
			userjsonObj1.classname = "com.wxtf.platform.plugins.org.OrgAction";
       		userjsonObj1.methodname = "listUser4Org";
			userjsonObj1.extenddata = '[{\"orgId\":\"'+val+'\",\"userName\":\"\"}]';
            userManager1 = $("#user1").ligerGrid({
            	url: _url, 
				parms: userjsonObj1,
				dataAction:'server',
				method:'post',async:false,
				width: '350', height:'325',
				checkbox: true,rownumbers:true,
                usePager:true,
                alternatingRow: true,//奇偶行显示效果
                columns: [
                	{ display: '已选用户', name: 'caption', width: 200, align: 'left' },
                	{ display: '用户ID', name: 'user_id', hide:true,width:1 }
                ]
           	});
           	
			userjsonObj2.classname = "com.wxtf.platform.plugins.org.OrgAction";
           	userjsonObj2.methodname = 'listUser';
			userjsonObj2.logicdata = '[{\"orgId\":\"'+val+'\",\"userName\":\"\",\"jigou\":\"\"}]';
            userManager2 = $("#user2").ligerGrid({
            	url: _url, 
				parms: userjsonObj2,
				dataAction:'server',
				method:'post',async:false,
				width: '350', height:'325',
				checkbox: true,rownumbers:true,
                usePager:true,
                alternatingRow: true,//奇偶行显示效果
                columns: [
                	{ display: '可选用户', name: 'caption', width: 200, align: 'left' },
                	{ display: '用户ID', name: 'user_id', hide:true, width:1 }
                ]
           	});
           	
           	
       	}
		//向组中添加用户
		function save_org_user(){
		    var save_row = manager.getSelectedRow();
			var save_rows = userManager2.getCheckedRows();
        	var str = "";
			$(save_rows).each(function (){
     	    	str += this.user_id + ",";
            });
            if(str == ''){
            	warningTip("请选择右侧人员");
            	return;
            }
          	str=str.substring(0, str.length-1);
          	var save_org_jsonObj = { 
				"classname": "",
				"methodname": "",
				"logicdata": [],
				"extenddata": []
			};
			save_org_jsonObj.classname = "com.wxtf.platform.plugins.org.OrgAction";
            save_org_jsonObj.methodname = "saveUser";
	     	save_org_jsonObj.logicdata = "[{\"orgId\":\""+save_row.id+"\",\"userId\":\""+str+"\"}]";
	      	callserver(save_org_jsonObj, sucess_guser, error);
		}
		//从组中删除用户
		function del_org_user(){
			var del_row = manager.getSelectedRow();
			var del_rows = userManager1.getCheckedRows();
        	var str = "";
        	$(del_rows).each(function (){
        	    str += this.user_id + ",";
        	});
			if(str == ''){
            	warningTip("请选择左侧人员");
            	return;
            }
            str=str.substring(0,str.length-1);
            var del_org_jsonObj = { 
				"classname": "",
				"methodname": "",
				"logicdata": [],
				"extenddata": []
			};
			del_org_jsonObj.classname = "com.wxtf.platform.plugins.org.OrgAction";
            del_org_jsonObj.methodname = "delUser";
		    del_org_jsonObj.logicdata = "[{\"orgId\":\""+del_row.id+"\",\"userId\":\""+str+"\"}]";
		    callserver(del_org_jsonObj, sucess_guser, error);
		}
		//部门成员：回调函数
		function sucess_guser(data){
		     userManager1.loadData();
		     userManager2.loadData();
		}
		function error(data){
			$.ligerDialog.error('很遗憾，操作失败！');
		}
		
		function search1(){
			var row = manager.getSelectedRow();
			userjsonObj1.extenddata = '[{\"orgId\":\"' + row.id + '\",\"userName\":\"' + $('#username1_query').val().trim() + '\",\"jigou\":\"' + $('#username1_query_jigou').val().trim() + '\"}]';
			userManager1.loadData();
		}
		
		function search2(){
			var row = manager.getSelectedRow();
			userjsonObj2.logicdata = '[{\"orgId\":\"' + row.id + '\",\"userName\":\"' + $('#username2_query').val().trim() + '\",\"jigou\":\"' + $('#username2_query_jigou').val().trim() + '\"}]';
		    userManager2.loadData();
		}
	</script>
	</head>

	<body>
		<div id="user-grid"></div>
		<div id="editDiv" style=" display: none;">
			<form id="mform">
       			<table class="form_table">
            		<tr>
		                <td class="form_label">部门名称:</td>
		                <td>
		                	<input name="text" id="text" style="width: 240px;" class="textbg1" onblur="toUpper(this);" onMouseOver="this.style.borderColor='#4fa8e6'" onMouseOut="this.style.borderColor='#A1A3A1'"/>
	                	</td>
		                <td align="left"><font color="red">*</font></td>
            		</tr>
		            <tr>
		                <td class="form_label">部门简码:</td>
		                <td>
		                	<input name="logic_code" id="logic_code" class="textbg1" style="width:240px;" onblur="toUpper(this);"  onMouseOver="this.style.borderColor='#4fa8e6'" onMouseOut="this.style.borderColor='#A1A3A1'"/>
	                	</td>
		                <td align="left"><font color="red">*</font></td>
		            </tr>
		                <tr>
		                <td class="form_label">负责人:</td>
		                <td>
		                	<input name="director_name" id="director_name" style="width: 240px;" class="textbg1" onMouseOver="this.style.borderColor='#4fa8e6'" onMouseOut="this.style.borderColor='#A1A3A1'"/>
	                	</td>
		                <td align="left"></td>
            		</tr>
		            <tr>
		                <td class="form_label">电话:</td>
		                <td>
		                	<input name="director_phone" id="director_phone" class="textbg1" type="text"  style="width:240px;" onMouseOver="this.style.borderColor='#4fa8e6'" onMouseOut="this.style.borderColor='#A1A3A1'"/>
	                	</td>
		                <td align="left"></td>
	            	</tr>
		            <tr>
		                <td class="form_label">手机:</td>
		                <td>
		                	<input name="director_mobile" id="director_mobile" class="textbg1" type="text" style="width:240px;" onMouseOver="this.style.borderColor='#4fa8e6'" onMouseOut="this.style.borderColor='#A1A3A1'"/>
	                	</td>
		                <td align="left"></td>
	            	</tr>
		            <tr>
		                <td class="form_label">邮箱:</td>
		                <td>
		                	<input name="director_email" id="director_email" class="textbg1" type="text" style="width:240px;"  onMouseOver="this.style.borderColor='#4fa8e6'" onMouseOut="this.style.borderColor='#A1A3A1'"/>
	                	</td>
		                <td align="left"></td>
	            	</tr>
		            <tr>
		                <td class="form_label" style="display: none;">简介:</td>
		                <td>
		                	<textarea id="action" name="action" class="textbg3"  style="width:240px; height:60px;display: none;" onMouseOver="this.style.borderColor='#4fa8e6'" onMouseOut="this.style.borderColor='#A1A3A1'"></textarea>
	                	</td>
		                <td align="left"></td>
		            </tr>
		            <tr>
		                <td class="form_label">排序号:</td>
		                <td>
		                	<input name="ordernum" type="text" id="ordernum" class="textbg1" style="width:240px;" onMouseOver="this.style.borderColor='#4fa8e6'" onMouseOut="this.style.borderColor='#A1A3A1'"/>
	                	</td>
		                <td align="left"></td>
		            </tr>
		        </table>
				<input type="hidden" id="id" name="id" value="" />
				<input type="hidden" id="dparent_id" name="dparent_id" value="" />
				<input type="hidden" id="levels" name="levels" value="" />
			</form>
		</div>
		<div id="orgUser" style="display: none;">
			<table align="center" border="0">
				<tr>
					<td> 
					    <div align="left">
							<br />
							用户：<input id="username1_query" />
							<input id="query" type="button" class="btn1" onclick="search1()" value="查询" />
						 </div>
					   <div id="user1"></div>
					</td>
					<td align="center">
						<input type="button" class="btn1" value="选择" onclick="save_org_user();"/>
						  <br/> <br/> <br/> <br/> <br/> <br/> <br/><br/><br/><br/>
						<input type="button" class="btn1" value="移除 " onclick="del_org_user();"/>
				   </td>
					<td >
						 <div align="left">
							    用户：<input id="username2_query"></input><br/>
							    机构：<input id="username2_query_jigou"></input>  
							 <input id="query2" type="button" class="btn1" onclick="search2()" value="查询" />
						 </div>
						<div id="user2"></div>
					</td>
				</tr>
			</table>
		</div>
	</body>
</html>


